﻿using System;
using System.Collections;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using BabyFood.Data;
using BabyFood.Entities;

public partial class Admin_LienHe_QLLienHe : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Pager1.CurrentIndex = 1;
            Bind_Data(Pager1.CurrentIndex);
        }
    }

    protected void Bind_Data(int PageNo)
    {
        try
        {
            int TotalRows = 0;
            string WhereClause = string.Empty;
            if(Common.StandardString(txtTieuDe.Text).Length > 0)
                WhereClause = "TieuDe LIKE N'%" + Common.StandardString(txtTieuDe.Text) + "%'";
            TList<LienHe> TList_LienHe = DataRepository.LienHeProvider.GetPaged(WhereClause, "NgayCapNhat DESC", PageNo - 1, Pager1.PageSize, out TotalRows);
            gridCatalog.DataSource = TList_LienHe;
            gridCatalog.DataBind();

            Pager1.ItemCount = TotalRows;
            if (TotalRows <= Pager1.PageSize)
                Pager1.Visible = false;
            else
                Pager1.Visible = true;
        }
        catch (Exception ex)
        {
            Common.WriteLog("QLLienHe.aspx.cs", "Bind_Data()", ex.ToString());
        }
    }

    protected void cmdTimKiem_Click(object sender, ImageClickEventArgs e)
    {
        Bind_Data(Pager1.CurrentIndex);
    }

    protected void Pager1_Command(object sender, CommandEventArgs e)
    {
        try
        {
            int currnetPageIndex = Convert.ToInt32(e.CommandArgument);
            Pager1.CurrentIndex = currnetPageIndex;
            Bind_Data(currnetPageIndex);
        }
        catch (Exception ex)
        {
            Common.WriteLog("QLLienHe.aspx.cs", "Pager1_Command()", ex.ToString());
        }
    }

    protected void gridCatalog_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.backgroundColor='WhiteSmoke';";
                e.Row.Attributes["onmouseout"] = "this.style.backgroundColor='White';";
            }
        }
        catch (Exception ex)
        {
            Common.WriteLog("QLLienHe.aspx.cs", "gridCatalog_RowDataBound()", ex.ToString());
        }
    }

    protected void gridCatalog_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Delete")
        {
            try
            {
                Guid IdLienHe = new Guid(e.CommandArgument.ToString());
                bool deleteStatus = DataRepository.LienHeProvider.Delete(IdLienHe);
                if (!deleteStatus)
                {
                    WebMessage.Show("Xóa Liên hệ thất bại");
                }
            }
            catch (Exception ex)
            {
                WebMessage.Show("Xóa Liên hệ thất bại");
                Common.WriteLog("QLLienHe.aspx.cs", "gridCatalog_RowCommand()", ex.ToString());
            }
        }
    }
    protected void gridCatalog_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        Bind_Data(Pager1.CurrentIndex);
    }
}
